<%@page import="com.ce.framework.util.StringUtil"%>
<%@page import="com.ce.framework.util.LogUtil"%>
<%@page import="com.ce.framework.context.FrameworkContext"%>
<%@ page language="java" pageEncoding="UTF-8"
%><%@ page import="java.util.*"
%><%@ page import="java.io.*"
%><%@ page import="java.net.*"
%><%@ page import="java.util.regex.*"
%><%@ page import="org.dom4j.*"
%><%@ page import="org.dom4j.io.*"
%><%!
String getParam(HttpServletRequest request,String name){
	String qs = request.getQueryString();
	if(qs==null)
		return null;
	qs = qs.replace("%26","&").replace("%3D","=");
	Pattern p = Pattern.compile(".*&"+name+"=([^&]+).*");
	Matcher m = p.matcher(qs);
	if(m.matches()){
		String res = m.group(1);
		try{
			res = URLDecoder.decode(res,"UTF-8");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return res;
	}
	return null;
}
List<String> getParamNames(HttpServletRequest request){
	List<String> list = new ArrayList<String>();
	String qs = request.getQueryString();
	if(qs==null)
		return list;
	qs = qs.replace("%26","&").replace("%3D","=");
	Pattern p = Pattern.compile("&([^=]+)=");
	Matcher m = p.matcher(qs);
	while(m.find()){
		list.add(m.group(1));
	}
	return list;
}
%><%
String path = request.getContextPath();
String type = request.getParameter("type");
String title = getParam(request,"title");
if(StringUtil.isBlank(type)){
	type = "pie";
}
if(StringUtil.isBlank(title)){
	title = "统计图";
}
List<String> params = getParamNames(request);
BufferedReader in = new BufferedReader(new FileReader(FrameworkContext.getAppPath()+"/js/amchart/am"+type+"_settings.xml"));
File f = new File(FrameworkContext.getAppPath()+"/js/amchart/am"+type+"_settings.xml");
if(f.exists()){
	SAXReader reader = new SAXReader();
	Document document = reader.read(new FileInputStream(f),"UTF-8");
    String param = null;
    String value = null;
    HashMap<String,String> map = new HashMap<String,String>();
    map.put("export_as_image/file",path+"/js/amchart/exportJpg.jsp?title="+title);
    map.put("strings/export_as_image","导出为图片");
    map.put("strings/collecting_data","正在准备数据");
	for(String p:params){
		param = p;
		value = getParam(request,param);
		if(StringUtil.isBlank(param)||StringUtil.isBlank(value)
				||"type".equals(param)||"title".equals(param)){
			continue;
		}
		map.put(param,value);
	}
	Iterator<String> it = map.keySet().iterator();
	while(it.hasNext()){
		param = it.next();
		value = map.get(param);
		List<Element> list = document.selectNodes("//settings/"+param);
		if(list==null||list.isEmpty()){
			LogUtil.logError("配置项中没有此XPATH：[//settings/"+param+"]");
			continue;
		}
		Element e = list.get(0);
		if(value.indexOf("<")>=0){
			e.addCDATA(value);
		}
		else{
			e.setText(value);
		}
	}
	XMLWriter writer = new XMLWriter(out);
	writer.write(document);
	writer.flush();
}
%>